package database;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.http.NameValuePair;

import android.util.Log;

public class dbMain {
	List<NameValuePair> params = new ArrayList<NameValuePair>();
	String url = "";
	String returnName = "";
	String returnValue="";
	String[] enemies;
	int success = -1;  // -1 = default / 0 = DB error / 1 = OK / 2 = Timeout
	public final String URL_USER = "http://cokabbs.bplaced.net/cok/db_user.php";
	public final String URL_CHAR = "http://cokabbs.bplaced.net/cok/db_character.php";
	public final String URL_GAME = "http://cokabbs.bplaced.net/cok/db_game.php";
	public final String URL_TEMP = "http://cokabbs.bplaced.net/cok/db_temp.php";
	public final static boolean DEBUG = false;

	public synchronized String getValue(String urlGetRow, String filterName, String filterValue, String returnName) {
		this.url = urlGetRow;
		this.returnName=returnName;
		this.params.add(new BasicNameValuePair("type", "'select'"));
		this.params.add(new BasicNameValuePair("return", "'"+returnName+"'"));
		this.params.add(new BasicNameValuePair("filter", "'"+filterName+"'"));
		this.params.add(new BasicNameValuePair("value", "'"+filterValue+"'"));
		request();
		return returnValue;
	}
	
	public synchronized String getDoubleValue(String urlGetRow, String filterName, String filterValue, String filterName2, String filterValue2, String returnName) {
		this.url = urlGetRow;
		this.returnName=returnName;
		this.params.add(new BasicNameValuePair("type", "'doubleselect'"));
		this.params.add(new BasicNameValuePair("return", "'"+returnName+"'"));
		this.params.add(new BasicNameValuePair("filter", "'"+filterName+"'"));
		this.params.add(new BasicNameValuePair("value", "'"+filterValue+"'"));
		this.params.add(new BasicNameValuePair("filter2", "'"+filterName2+"'"));
		this.params.add(new BasicNameValuePair("value2", "'"+filterValue2+"'"));
		request();
		return returnValue;
	}
	
	public synchronized String getTemp(int id, int runde, String returnName) {
		this.url = URL_TEMP;
		this.returnName=returnName;
		this.params.add(new BasicNameValuePair("type", "'doubleselect'"));
		this.params.add(new BasicNameValuePair("return", "'"+returnName+"'"));
		this.params.add(new BasicNameValuePair("tempID", "'"+id+"'"));
		this.params.add(new BasicNameValuePair("trunde", "'"+runde+"'"));
		request();
		return returnValue;
	}
	
	public synchronized void updateValue(String url, String filterName, String filterValue, String setName, String setValue) {
		this.url = url;
		this.params.add(new BasicNameValuePair("type", "'update'"));
		this.params.add(new BasicNameValuePair("filter", "'"+filterName+"'"));
		this.params.add(new BasicNameValuePair("value", "'"+filterValue+"'"));
		this.params.add(new BasicNameValuePair("nfilter", "'"+setName+"'"));
		this.params.add(new BasicNameValuePair("nvalue", "'"+setValue+"'"));
		request();
	}
	
	public synchronized void updateDoubleValue(String url, String filterName, String filterValue,String filterName2, String filterValue2, String setName, String setValue) {
		this.url = url;
		this.params.add(new BasicNameValuePair("type", "'doubleupdate'"));
		this.params.add(new BasicNameValuePair("filter", "'"+filterName+"'"));
		this.params.add(new BasicNameValuePair("value", "'"+filterValue+"'"));
		this.params.add(new BasicNameValuePair("filter2", "'"+filterName2+"'"));
		this.params.add(new BasicNameValuePair("value2", "'"+filterValue2+"'"));
		this.params.add(new BasicNameValuePair("nfilter", "'"+setName+"'"));
		this.params.add(new BasicNameValuePair("nvalue", "'"+setValue+"'"));
		request();
	}
	
	public synchronized void updateTemp(int tempID, int runde, String filterName, int value) {
		this.url = URL_TEMP;
		this.params.add(new BasicNameValuePair("type", "'doubleupdate'"));
		this.params.add(new BasicNameValuePair("tempID", "'"+tempID+"'"));
		this.params.add(new BasicNameValuePair("trunde", "'"+runde+"'"));
		this.params.add(new BasicNameValuePair("nfilter", "'"+filterName+"'"));
		this.params.add(new BasicNameValuePair("nvalue", "'"+value+"'"));
		request();
	}
	
	public synchronized void addUser(String name, String password) {
		this.url = URL_USER;
		this.params.add(new BasicNameValuePair("type", "'insert'"));
		this.params.add(new BasicNameValuePair("name", "'"+name+"'"));
		this.params.add(new BasicNameValuePair("passwd", "'"+password+"'"));
		request();
	}
	
	public synchronized void addCharacter(String userID, String name, String charClass, String str,
			String agi, String vit, String intel, String lvl, String xp,
			String premade) {
		this.url = URL_CHAR;
		this.params.add(new BasicNameValuePair("type", "'insert'"));
		this.params.add(new BasicNameValuePair("uid", "'"+userID+"'"));
		this.params.add(new BasicNameValuePair("name", "'"+name+"'"));
		this.params.add(new BasicNameValuePair("class", "'"+charClass+"'"));
		this.params.add(new BasicNameValuePair("str", "'"+str+"'"));
		this.params.add(new BasicNameValuePair("agi", "'"+agi+"'"));
		this.params.add(new BasicNameValuePair("vit", "'"+vit+"'"));
		this.params.add(new BasicNameValuePair("intel", "'"+intel+"'"));
		this.params.add(new BasicNameValuePair("lvl", "'"+lvl+"'"));
		this.params.add(new BasicNameValuePair("xp", "'"+xp+"'"));
		this.params.add(new BasicNameValuePair("premade", "'"+premade+"'"));
		request();
	}
	
	public synchronized void addGame(String gameAbility, String gameDmg) {
		this.url = URL_GAME;
		this.params.add(new BasicNameValuePair("type", "'insert'"));
		this.params.add(new BasicNameValuePair("gabil", "'"+gameAbility+"'"));
		this.params.add(new BasicNameValuePair("gdmg", "'"+gameDmg+"'"));
		request();
	}
	public synchronized void addRound(int tempID, int trunde, int punkteS1, int punkteS2, int faehigkeitS1, int faehigkeitS2) {
		this.url = URL_TEMP;
		this.params.add(new BasicNameValuePair("type", "'insert'"));
		this.params.add(new BasicNameValuePair("tempID", "'"+tempID+"'"));
		this.params.add(new BasicNameValuePair("trunde", "'"+trunde+"'"));
		this.params.add(new BasicNameValuePair("punkte1", "'"+punkteS1+"'"));
		this.params.add(new BasicNameValuePair("punkte2", "'"+punkteS2+"'"));
		this.params.add(new BasicNameValuePair("faehig1", "'"+faehigkeitS1+"'"));
		this.params.add(new BasicNameValuePair("faehig2", "'"+faehigkeitS2+"'"));
		request();
	}
	
	public synchronized void addTemp(int temp) {
		this.url = URL_TEMP;
		this.params.add(new BasicNameValuePair("type", "'create'"));
		this.params.add(new BasicNameValuePair("tempID", "'"+temp+"'"));
		request();
	}
	
	public synchronized String[] getEnemies() {
		this.url = URL_CHAR;
		this.returnName="activeEnemies";
		this.params.add(new BasicNameValuePair("type", "activeenemies"));
		request();
		if (enemies != null) {
			return enemies;
		} else {
			return new String[]{""};
		}
	}
	
	public synchronized void deleteRow(String url, String filterName, String filterValue) {
		this.url = url;
		this.params.add(new BasicNameValuePair("type", "'delete'"));
		this.params.add(new BasicNameValuePair("filter", "'"+filterName+"'"));
		this.params.add(new BasicNameValuePair("value", "'"+filterValue+"'"));
		request();
	}
	
	public synchronized void dropTable(String url, String filterName, int gID) {
		this.url = url;
		this.params.add(new BasicNameValuePair("type", "'drop'"));
		this.params.add(new BasicNameValuePair("filter", "'"+filterName+"'"));
		this.params.add(new BasicNameValuePair("value", "'"+gID+"'"));
		request();
	}
	
	private synchronized void request() {
		startThread();
		try {
			for (int i = 0; i < 20; i++) {
				if (DEBUG) Log.e("DB_REQUEST_THREAD", "WARTE " +i);
				Thread.sleep(250);
				if (success != -1) {
					success = -1;
					break;
				}
				if (i == 19) {
					success = 2;
					Log.d("Database", "Connection timed out.");
				}
			}
		} catch (InterruptedException e) {}
		params.clear();
	}

	private void startThread() {
		Thread networkThread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					executeJason(url, params);
					Thread.currentThread().interrupt();
					return;
				} catch (Exception e) {}
			}

			private void executeJason(String url, List<NameValuePair> params) {
				try {
					JSONParser jP = new JSONParser();
					JSONObject json = jP.makeHttpRequest(url, "GET", params);
					success = json.getInt("success");
					if (DEBUG) Log.e("DB_SUCCESS", Integer.toString(success));
					if (success == 1) {
						if (returnName.equals("activeEnemies")) {
							int rows = json.getInt("rows");
							if (DEBUG) Log.e("DB_ENEMIES", Integer.toString(rows));
							enemies = new String[rows];
							for (int i = 0; i < rows; i++) {
								enemies[i] = json.getString("result"+Integer.toString(i)+"");
							}
						} else {
							returnValue = json.getString(returnName);
							if (DEBUG) Log.e("DB_RETURNVALUE", returnValue);
						}
					} else {
						returnValue = "";
					}
				} catch (JSONException e) { if (DEBUG) Log.e("JSON ERROR", e.toString()); }
			}
		});
		networkThread.start();
	}
}
